<?php
/**
 * ============================================================================
 * Copyright (c) 2015-2018 贵州大师兄信息技术有限公司 All rights reserved.
 * siteַ: http://www.dsxcms.com
 * ============================================================================
 * @author:     David Song<songdewei@163.com>
 * @version:    v1.0.0
 * ---------------------------------------------
 * Date: 2018/10/29
 * Time: 10:18 PM
 */

namespace App\Library\Mall\Seller;


use App\Events\OrderEvent;
use App\Exceptions\UserRequestException;
use App\Library\AuthenticatedUser;
use App\Models\Express;
use App\Models\Order;
use Illuminate\Http\Request;

trait SoldManagers
{
    use AuthenticatedUser;

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws UserRequestException
     */
    public function send(Request $request)
    {
        $order_id = $request->input('order_id');
        $order = $this->user()->solds()->find($order_id);
        if ($order) {
            if ($order->shipping_state) {
                return $this->sendOrderResponse($request, $order);
            }

            $shipping_type = $request->input('shipping_type', 1);
            if ($shipping_type == 1) {
                $express_id = $request->input('express_id');
                $express_no = $request->input('express_no');

                if ($express_id && $express_no) {
                    $express = Express::find($express_id);
                    $order->shipping->update([
                        'shipping_type' => $shipping_type,
                        'express_code' => $express->code,
                        'express_name' => $express->name,
                        'express_no' => $express_no
                    ]);
                } else {
                    throw new UserRequestException(trans('sysmessage.invalid parameter'));
                }
            } else {
                $order->shipping->update(['shipping_type' => $shipping_type]);
            }

            $order->changeToSend();

            $order->actions()->create([
                'uid' => $this->user()->uid,
                'username' => $this->user()->username,
                'content' => trans('order.delivery success')
            ]);

            event(new OrderEvent($order, 'send'));

            return $this->sendOrderResponse($request, $order);
        } else {
            throw new UserRequestException(trans('order.order does not exist'), 404);
        }
    }

    /**
     * @param Request $request
     * @param Order $order
     * @return \Illuminate\Http\JsonResponse
     */
    protected function sendOrderResponse(Request $request, Order $order)
    {
        return ajaxReturn(['order' => $order]);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function delete(Request $request){
        $order_id = $request->input('order_id');
        $order = $this->user()->solds()->find($order_id);
        $order->seller_deleted = 1;
        $order->save();
        return $this->deletedOrderResponse($request, $order);
    }

    /**
     * @param Request $request
     * @param $order
     * @return \Illuminate\Http\JsonResponse
     */
    protected function deletedOrderResponse(Request $request, $order){
        return ajaxReturn(['order'=>$order]);
    }
}
